/*
 * Copyright (c) 2019. seven studio. @chengdu
 * seven studio in chengdu by lb
 * this project for snmp tool
 */

package com.xlx.openLink.threadtask;

import com.xlx.openLink.dao.DeviceMapper;
import com.xlx.openLink.entity.pojo.Alarm;
import com.xlx.openLink.entity.pojo.Device;
import com.xlx.openLink.service.AlarmService;
import com.xlx.openLink.service.DeviceService;
import com.xlx.openLink.threadtask.workservice.SearchDeviceService;
import com.xlx.openLink.uitls.Commuitls;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * 发现设备定时任务
 */

@Component
@Slf4j
public class DiscoverDevTask {

    @Autowired
    private DeviceMapper deviceMapper;

    @Autowired
    private DeviceService deviceService;
    @Autowired
    private AlarmService alarmService;
    @Autowired
    private SearchDeviceService searchDeviceService;

    //@Scheduled(fixedDelay = 60 * 1000)
    @Transactional
    public void discoverDev() {
        log.info("执行更新设备开始----------------");
        long start = System.currentTimeMillis();
        BlockingDeque<Device> blockingDeque = new LinkedBlockingDeque<Device>();
        BlockingDeque<Alarm> alarmBlockingDeque = new LinkedBlockingDeque<Alarm>();
        List<Device> list = deviceMapper.selectSnmpDev();
        try {
            searchDeviceService.SearchDevice(list, blockingDeque, alarmBlockingDeque);
            if (blockingDeque.size() > 0) {
                List<Device> upList = Commuitls.que2List(blockingDeque);
                deviceService.updateBatch(upList);
                log.info("更新设备完成");
            }
            if (alarmBlockingDeque.size() > 0) {
                List<Alarm> alarmList = Commuitls.que2List(alarmBlockingDeque);
                alarmService.insertBatch(alarmList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        long time = System.currentTimeMillis() - start;
        double stime = (double) time / 1000;
        log.info("更新设备--------------------time is " + stime + "s");
    }


}
